package cn.iocoder.yudao.module.mqtt.controller.admin.devicereport;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.devicereport.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicereport.DeviceReportDO;
import cn.iocoder.yudao.module.mqtt.service.devicereport.DeviceReportService;

@Tag(name = "管理后台 - MQTT报表分类")
@RestController
@RequestMapping("/mqtt/device-report")
@Validated
public class DeviceReportController {

    @Resource
    private DeviceReportService deviceReportService;

    @PostMapping("/create")
    @Operation(summary = "创建MQTT报表分类")
    @PreAuthorize("@ss.hasPermission('mqtt:device-report:create')")
    public CommonResult<Long> createDeviceReport(@Valid @RequestBody DeviceReportSaveReqVO createReqVO) {
        
        return success(deviceReportService.createDeviceReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新MQTT报表分类")
    @PreAuthorize("@ss.hasPermission('mqtt:device-report:update')")
    public CommonResult<Boolean> updateDeviceReport(@Valid @RequestBody DeviceReportSaveReqVO updateReqVO) {
        deviceReportService.updateDeviceReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除MQTT报表分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:device-report:delete')")
    public CommonResult<Boolean> deleteDeviceReport(@RequestParam("id") Long id) {
        deviceReportService.deleteDeviceReport(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除MQTT报表分类")
                @PreAuthorize("@ss.hasPermission('mqtt:device-report:delete')")
    public CommonResult<Boolean> deleteDeviceReportList(@RequestParam("ids") List<Long> ids) {
        deviceReportService.deleteDeviceReportListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得MQTT报表分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:device-report:query')")
    public CommonResult<DeviceReportRespVO> getDeviceReport(@RequestParam("id") Long id) {
        DeviceReportDO deviceReport = deviceReportService.getDeviceReport(id);
        return success(BeanUtils.toBean(deviceReport, DeviceReportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得MQTT报表分类分页")
    @PreAuthorize("@ss.hasPermission('mqtt:device-report:query')")
    public CommonResult<PageResult<DeviceReportRespVO>> getDeviceReportPage(@Valid DeviceReportPageReqVO pageReqVO) {
        PageResult<DeviceReportDO> pageResult = deviceReportService.getDeviceReportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceReportRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出MQTT报表分类 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:device-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceReportExcel(@Valid DeviceReportPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceReportDO> list = deviceReportService.getDeviceReportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "MQTT报表分类.xls", "数据", DeviceReportRespVO.class,
                        BeanUtils.toBean(list, DeviceReportRespVO.class));
    }

}